package cn.kilo.trigger.http.manage;


import cn.kilo.domain.model.order.entity.MGuardEntity;
import cn.kilo.domain.model.order.entity.MRiderEntity;
import cn.kilo.domain.model.order.entity.MSorterEntity;
import cn.kilo.domain.model.order.entity.MUserEntity;
import cn.kilo.domain.model.order.res.MGuardRes;
import cn.kilo.domain.model.order.res.MRiderRes;
import cn.kilo.domain.model.order.res.MSorterRes;
import cn.kilo.domain.model.order.res.MUserRes;
import cn.kilo.domain.service.infterface.IOrderStrategyService;
import cn.kilo.trigger.http.manage.Dto.MGuardDTO;
import cn.kilo.trigger.http.manage.Dto.MRiderDTO;
import cn.kilo.trigger.http.manage.Dto.MSorterDTO;
import cn.kilo.trigger.http.manage.Dto.MUserDTO;
import cn.kilo.types.enums.ResponseCode;
import cn.kilo.types.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author kilok
 */
@RestController
@RequestMapping("/userManager")
@CrossOrigin("*")
@Slf4j
public class UserManager {
    // TODO: 2024/7/9 通过视图的方式实现多表的查询
    @Resource
    private IOrderStrategyService orderService;

    // TODO: 2024/7/8 实现多表查询

    /**
     * 人员管理获取全部用户
     * @return
     */
    @GetMapping("/getAllUser")
    public Response<?> getAllUser() {
        try {
            List<MUserRes> ans = orderService.getAllUsers();
            return Response.<List<MUserRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("用户信息获取失败")
                    .build();
        }
    }

    /**
     * 条件查询用户
     * @param mUserDTO
     * @return
     */
    @PostMapping("/getUserBy")
    public Response<?> getUserBy(@RequestBody MUserDTO mUserDTO) {
        try {
            List<MUserRes> ans=orderService.getUserBy(MUserEntity.builder()
                    .orderId(mUserDTO.getOrderId())
                    .userName(mUserDTO.getUserName())
                    .end(mUserDTO.getEnd())
                    .orderNumber(mUserDTO.getOrderNumber())
                    .orderCode(mUserDTO.getOrderCode())
                    .build());
            return Response.<List<MUserRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("条件查询用户信息失败")
                    .build();
        }
    }

    /**
     * 骑手管理
     * @return
     */
    @GetMapping("/getAllRider")
    public Response<?> getAllRider() {
        try {
            List<MRiderRes> ans=orderService.getAllRider();
            return Response.<List<MRiderRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("骑手信息获取失败")
                    .build();
        }
    }

    /**
     * 条件查询骑手
     * @param mRiderDTO
     * @return
     */
    @PostMapping("/getRiderBy")
    public Response<?> getUserBy(@RequestBody MRiderDTO mRiderDTO) {
        try {
            List<MRiderRes> ans=orderService.getRiderBy(MRiderEntity.builder()
                            .sorter1Name(mRiderDTO.getSorter1Name())
                            .orderNumber(mRiderDTO.getOrderNumber())
                            .end(mRiderDTO.getEnd())
                            .orderCode(mRiderDTO.getOrderCode())
                            .orderId(mRiderDTO.getOrderId())
                    .build());
            return Response.<List<MRiderRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("条件查询骑手信息失败")
                    .build();
        }
    }

    /**
     * 获取分拣员信息
     * @return
     */
    @GetMapping("/getAllGuard")
    public Response<?> getAllGuard() {
        try {
            List<MGuardRes> ans=orderService.getAllGuard();
            return Response.<List<MGuardRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("分拣员信息获取失败")
                    .build();
        }
    }

    /**
     * 条件查询分拣员
     * @param mGuardDTO
     * @return
     */
    @PostMapping("/getGuardBy")
    public Response<?> getGuardBy(@RequestBody MGuardDTO mGuardDTO) {
        try {
            List<MGuardRes> ans=orderService.getGuardBy(MGuardEntity.builder()
                            .guardName(mGuardDTO.getGuardName())
                            .end(mGuardDTO.getEnd())
                            .orderId(mGuardDTO.getOrderId())
                    .build());
            return Response.<List<MGuardRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("条件查询分拣员信息失败")
                    .build();
        }
    }

    /**
     * 查询所有派送员
     * @return
     */
    @GetMapping("/getAllSorter")
    public Response<?> getAllSorter() {
        try {
            List<MSorterRes> ans=orderService.getAllSorter();
            return Response.<List<MSorterRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("派送员信息获取失败")
                    .build();
        }
    }

    /**
     * 条件查询派送员
     * @param mSorterDTO
     * @return
     */
    @PostMapping("/getSorterBy")
    public Response<?> getSorterBy(@RequestBody MSorterDTO mSorterDTO) {
        try {
            List<MSorterRes> ans=orderService.getSorterBy(MSorterEntity.builder()
                            .sorter2Name(mSorterDTO.getSorter2Name())
                            .end(mSorterDTO.getEnd())
                            .orderCode(mSorterDTO.getOrderCode())
                            .orderNumber(mSorterDTO.getOrderNumber())
                            .orderId(mSorterDTO.getOrderId())
                    .build());
            return Response.<List<MSorterRes>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(ans)
                    .build();
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("条件查询派送员信息失败")
                    .build();
        }
    }




    @GetMapping("/")
    public Response<?> test() {
        try {
            return null;
        } catch (Exception e) {
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("")
                    .build();
        }
    }
}
